package com.milks.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.milks.bean.Pager;
import com.milks.entity.Product;
import com.milks.entity.ProductCategory;
import com.milks.service.BrandService;
import com.milks.service.ProductCategoryService;
import com.milks.service.ProductService;
import com.milks.utils.UploadPicture;

@Controller
@RequestMapping("/admin/productCategory")
public class ProductCategoryAdminController {
	
	@Resource
	private ProductService productService;
	@Resource
	private ProductCategoryService productCategoryService;
	@Resource
	private BrandService brandService;
	
	@RequestMapping(value="/productCategoryList",method=RequestMethod.GET)
	public String productCategoryList(Model model) {
		List<ProductCategory> productCategoryTreeList = productCategoryService.getProductCategoryTreeList();
		model.addAttribute("productCategoryTreeList", productCategoryTreeList);
		return "admin/product_category_list";
	}
	
	@RequestMapping(value="/productCategoryAdd",method=RequestMethod.GET)
	public String addProductCategory(Model model) {
		List<ProductCategory> productCategoryTreeList = productCategoryService.getProductCategoryTreeList();
		model.addAttribute("productCategoryTreeList", productCategoryTreeList);
		model.addAttribute("isAdd", "add");
		return "admin/product_category_input";
	}
	
	//可能是由于整合FreeMaker的原因，不能够正常自动组装productCategory参数
	@RequestMapping(value="/productCategoryAdd",method=RequestMethod.POST)
	public String addProductCategory(Model model,@ModelAttribute ProductCategory productCategory,HttpServletRequest request) {
		String parentId = request.getParameter("parentId");
		if (StringUtils.isNotEmpty(parentId)) {
			ProductCategory parent = productCategoryService.get(parentId);
			productCategory.setParent(parent);
		} else {
			productCategory.setParent(null); 
		}
		productCategoryService.save(productCategory);
		List<ProductCategory> productCategoryTreeList = productCategoryService.getProductCategoryTreeList();
		model.addAttribute("productCategoryTreeList", productCategoryTreeList);
		return "redirect:productCategoryList";
	}
	
	@RequestMapping(value="/productCategoryUpdate",method=RequestMethod.GET)
	public String updateProductCategory(Model model,String id) {
		List<ProductCategory> productCategoryTreeList = productCategoryService.getProductCategoryTreeList();
		model.addAttribute("productCategoryTreeList", productCategoryTreeList);
		ProductCategory productCategory =productCategoryService.get(id);
		model.addAttribute("productCategory", productCategory);
		//是更新页面还是添加页面的标志
		model.addAttribute("isAdd", "edit");
		return "admin/product_category_input";
	}
	
	@RequestMapping(value="/productCategoryUpdate",method=RequestMethod.POST)
	public String updateProductCategory(Model model,ProductCategory productCategory,HttpServletRequest request) {
		ProductCategory persistent = productCategoryService.get(productCategory.getId());
		BeanUtils.copyProperties(productCategory, persistent, new String[]{"id", "createDate", "modifyDate", "path", "parent", "children", "productSet"});
		productCategoryService.update(persistent);
		return "redirect:productCategoryList";
	}
	
	@RequestMapping(value="/uploadPicture")
	@ResponseBody
	public List<String> uploadPicture(Model model,HttpServletRequest request, HttpServletResponse response) {
		return UploadPicture.uplaod(request, response);
	}
	
	@RequestMapping(value="/productCategoryDelete",method=RequestMethod.GET)
	public String deleteProductCategory(Model model,String id) {
		
		ProductCategory productCategory = productCategoryService.load(id);
		Set<ProductCategory> childrenProductCategorySet = productCategory.getChildren();
		if (childrenProductCategorySet != null && childrenProductCategorySet.size() > 0) {
			model.addAttribute("errorContent", "此商品分类存在下级分类，删除失败!");
			return "common/error_page";
		}
		Set<Product> productSet = productCategory.getProductSet();
		if (productSet != null && productSet.size() > 0) {
			model.addAttribute("errorContent", "此商品分类下存在商品，删除失败!");
			return "common/error_page";
		}
		productCategoryService.delete(id);
		return "redirect:productCategoryList";
	}
}
